package sisi.suppliers;

import java.util.List;

import javax.persistence.EntityManager;
/**
 * PortssupplierController.java - Controller class for the <i>supplier ports database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 10/12/2013
 * 
 */
public class PortssupplierController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Portssupplier[] get(String cerca) {
		List<Portssupplier> lista = getList(cerca);
			return lista.toArray(new Portssupplier[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Portssupplier> getList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.portname) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Portssupplier as c " + cWhere 
					+ " order by c.portname ");

			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}

			List<Portssupplier> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	
	@SuppressWarnings("unchecked")
	public List<Portssupplier> getListxIdSupplier(Integer idSupplier) {

		String cWhere = "";
		cWhere = " WHERE c.suppid = :cerca ";

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Portssupplier as c " + cWhere 
					+ " order by c.seq ");

			q.setParameter("cerca", idSupplier );

			List<Portssupplier> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Portssupplier> getListXPortName(String portName) {

		String cWhere = "";
		cWhere = " WHERE TRIM( BOTH FROM UPPER(c.portname) ) = :portname ";

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Portssupplier as c " + cWhere 
					+ " order by c.suppid "); 

			q.setParameter("portname", portName.trim().toUpperCase() );
			List<Portssupplier> lista = q.getResultList();
			
			return lista;
		} finally {
			em.close();
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Portssupplier> getListXIdPort(Integer idPort) {

		String cWhere = "";
		cWhere = " WHERE c.portid =  "+idPort;

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Portssupplier as c " + cWhere 
					+ " order by c.suppid "); 

			List<Portssupplier> lista = q.getResultList();
			
			return lista;
		} finally {
			em.close();
		}
	}
	
	public Portssupplier getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Portssupplier portssupplierx = em.find(Portssupplier.class, id);
			return portssupplierx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Portssupplier add(Portssupplier portssupplier) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(portssupplier);
			em.getTransaction().commit();
			portssupplier = refresh(portssupplier);
		} finally {
			em.close();
			return portssupplier;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Portssupplier portssupplier) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Portssupplier portssupplierx = em.find(Portssupplier.class, portssupplier.getId());
			portssupplierx.setId(portssupplier.getId());
			portssupplierx.setCountry(portssupplier.getCountry());
			portssupplierx.setSeq(portssupplier.getSeq());
			portssupplierx.setLogicDelete(portssupplier.getLogicDelete());
			portssupplierx.setPortid(portssupplier.getPortid());
			portssupplierx.setFuel(portssupplier.getFuel());
			portssupplierx.setOil(portssupplier.getOil());
			portssupplierx.setPortname(portssupplier.getPortname());
			portssupplierx.setSupname(portssupplier.getSupname());
			portssupplierx.setSuppid(portssupplier.getSuppid());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Portssupplier refresh(Portssupplier portssupplier) {

		Portssupplier portssupplierx = portssupplier;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			portssupplierx = em.find(Portssupplier.class, portssupplier.getId());
			em.flush();
			em.refresh(portssupplierx);
		} finally {
			em.close();
			return portssupplierx;
		}
}

@SuppressWarnings("finally")
public boolean remove(Portssupplier portssupplier) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Portssupplier portssupplierx = em.find(Portssupplier.class, portssupplier.getId());
			em.remove(portssupplierx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
//
// fine
